using System;
using Db4objects.Db4o;
using Db4objects.Db4o.Linq;

namespace SharedObjects
{
	public sealed class Utils
	{
		private static bool CheckIsValidAccountNumber(ref IObjectContainer db, long accountNumber, ulong systemId)
		{
			if (accountNumber == 0)
				return false;
			if ((from BankAccount ba in db where ba.SystemId == systemId && ba.AccountNumber == accountNumber select ba).Count() != 0)
				return false;
			return true;
		}

		public static long GetBankAccountNumber(IObjectContainer db, ulong systemId)
		{
			long accountNumber = 0;
			while (!CheckIsValidAccountNumber(ref db, accountNumber, systemId))
			{
				byte[] bytes = Guid.NewGuid().ToByteArray();
				for (int i = 0; i < 4; i++)
					accountNumber += BitConverter.ToUInt32(bytes, i * 4);
			}
			return accountNumber;
		}

	}
}